package xyz.bali16.module.netdisk.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.netdisk.utils.PageUtils;
import xyz.bali16.module.netdisk.service.NetdiskService;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static xyz.bali16.application.core.model.Result.success;

/**
 * @author bali2 on 7/11/2021.
 * @version 1.0
 */

/**
 * 我们应该协约好，这里只提供接口对外。
 * 外部目前使用网盘服务的有博客系统的上传缩略图功能
 * 这里已经提供了80用户/仓库/文件夹给博客图片使用
 * 而博客那边应根据这里的接口参数进一步封装博客使用的网盘调用工具类 - 到时候形成模板,形成抽象工具类,方便复用
 */
@RestController
@RequestMapping("/api2/netdisk")
public class NetdiskWebController {
    @Autowired
    protected NetdiskService netdiskService;


    /**
     * 网盘对外上传接口
     *
     * @param file          上传的文件
     * @param fileUserId    问你就
     * @param fileStorageId 文件仓库ID
     * @param fileFolderId  文件夹ID
     * @param request
     * @return
     */
    @PostMapping("/upload")

    @ApiOperation("网盘对外上传接口")
    //@RequiresPermissions("netdisk:api:upload")
    public Result<String> uploadFile(MultipartFile file, String fileUserId, String fileStorageId, String fileFolderId, HttpServletRequest request) {
        return netdiskService.uploadFile(file, fileUserId, fileStorageId, fileFolderId, request);
    }

    /**
     * 网盘对外下载接口
     *
     * @param fileUserId
     * @param fileStorageId
     * @param fileId
     * @param request
     * @param response
     */
    @GetMapping("/download")
    @ApiOperation("网盘对外下载接口")
    //@RequiresPermissions("netdisk:api:download")
    public void downloadFile(String fileUserId, String fileStorageId, String fileId, HttpServletRequest request, HttpServletResponse response) {
        netdiskService.downloadFile(fileUserId, fileStorageId, fileId, request, response);
    }

    @GetMapping

    @ApiOperation("网盘对外列表数据")
    //@RequiresPermissions("netdisk:api:query")
    public Result<PageUtils> list(
            @RequestParam(value = "fileStorageId", required = false) String fileStorageId,
            @RequestParam(value = "fileFolderId", required = false) String fileFolderId,
            @RequestParam(value = "curPage", required = false, defaultValue = "1") Integer curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        return success(netdiskService.list(fileStorageId, fileFolderId, curPage, pageSize));
    }
}
